#!/usr/local/bin/perl -w

use App::Options (
    options => [qw(dbdriver dbclass dbhost dbname dbuser dbpass)],
    option => {
        dbclass  => { default => "App::Repository::MySQL", },
        dbdriver => { default => "mysql", },
        dbhost   => { default => "localhost", },
        dbname   => { default => "test", },
        dbuser   => { default => "", },
        dbpass   => { default => "", },
    },
);

use Test::More qw(no_plan);
use lib "../App-Context/lib";
use lib "../../App-Context/lib";
use lib "lib";
use lib "../lib";
use lib ".";
use lib "t";

use App;
use App::Repository;
use RepositoryTestUtils qw(create_table_test_person drop_table_test_person populate_table_test_person);
use strict;

if (!$App::options{dbuser}) {
    ok(1, "No dbuser given. Tests assumed OK. (add dbuser=xxx and dbpass=yyy to app.conf in 't' directory)");
    exit(0);
}

my $context = App->context(
    conf_file => "",
    conf => {
        Repository => {
            default => {
                class => $App::options{dbclass},
                dbdriver => $App::options{dbdriver},
                dbhost => $App::options{dbhost},
                dbname => $App::options{dbname},
                dbuser => $App::options{dbuser},
                dbpass => $App::options{dbpass},
                table => {
                    test_person => {
                        primary_key => ["person_id"],
                        column => {
                            age_sq => {
                                expr => "{age}*{age}",
                            },
                            public_age => {
                                expr => "{age}-10",
                            },
                            wisdom_score => {
                                expr => "{age_sq}/20",  # an expression of expressions
                            },
                        },
                    },
                },
            },
        },
    },
    debug_sql => $App::options{debug_sql},
    trace => $App::options{trace},
);

my $rep = $context->repository();

###########################################################################################
# Set Up the Database for the Tests
###########################################################################################
&drop_table_test_person($rep);
&create_table_test_person($rep);
&populate_table_test_person($rep);

###########################################################################
# DATA ACCESS TESTS
###########################################################################
my ($person_id, $first_name, $last_name, $address, $city, $state, $zip, $country);
my ($home_phone, $work_phone, $email_address, $gender, $birth_dt, $age);

my $columns = [ "person_id", "age", "first_name", "gender", "state" ];

#####################################################################
#  $value  = $rep->get ($table, $key,     $col,   \%options);
#  $rep->set($table, $key,     $col,   $value,    \%options);
#####################################################################
{
    my ($row, $rows, $hash, $hashes, $objects, $object, $nrows);
    my ($age, $age_sq, $wisdom_score, $public_age);
    $age = $rep->get("test_person",1,"age");
    is($age, 39, "get(age)");
    $age_sq = $rep->get("test_person",1,"age_sq");
    is($age_sq, 1521, "get(age_sq)");
    $wisdom_score = $rep->get("test_person",1,"wisdom_score");
    is($wisdom_score, 76.05, "get(wisdom_score)");
    my @cols = ("wisdom_score");
    $rows = $rep->get_rows("test_person",1,\@cols);
    $row = $rows->[0];
    is($row->[0], 76.05, "get_rows(wisdom_score)");
    $hashes = $rep->get_hashes("test_person",1,\@cols);
    $hash = $hashes->[0];
    is($hash->{wisdom_score}, 76.05, "get_hashes(wisdom_score)");
    $objects = $rep->get_objects("test_person",1,\@cols);
    $object = $objects->[0];
    is($object->{wisdom_score}, 76.05, "get_objects(wisdom_score)");
}

